package SiTuQingYou.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;

import SiTuQingYou.dao.CityDao;
import SiTuQingYou.dao.SceneClassDao;
import SiTuQingYou.dao.SceneDao;
import SiTuQingYou.dao.ScenePhotoDao;
import SiTuQingYou.util.FileRename;
import SiTuQingYou.util.Pager;
import SiTuQingYou.vo.City;
import SiTuQingYou.vo.Scene;
import SiTuQingYou.vo.SceneClass;

/**
 * Servlet implementation class ScenehomeServlet
 */
@WebServlet("/ScenehomeServlet")
public class ScenehomeServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScenehomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void sceneinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    	//SceneDao scene = SceneDao.getscened().getSceneById(id);
    	String sceneid = request.getParameter("id");
    	int scid = Integer.parseInt(sceneid);
    	Scene scene = SceneDao.getscened().getSceneById(scid);
    	request.setAttribute("scene", scene);
    }
	protected void allcity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//读城市
		CityDao city =  CityDao.getcityd(); 
		
		ArrayList<City> allcity = city.allcity();
		request.setAttribute("allcity", allcity);
	}
	protected void getpicture(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String sceneid = request.getParameter("id");
    	int scid = Integer.parseInt(sceneid);
    	ArrayList<String> pics = ScenePhotoDao.getspd().getallphotobyscenesid(scid);
    	request.setAttribute("pics", pics);
	}
	protected void sceneclass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//读分类
		SceneClassDao scenec = SceneClassDao.getscd();  
		 ArrayList<SceneClass> allscene = scenec.sceneclass();
		 request.setAttribute("allscene", allscene);
	}
	protected void newscene(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//新增一个景点
		int cityid = Integer.parseInt(request.getParameter("cityid"));
		String scenename = request.getParameter("scenename");
		int themeid = Integer.parseInt(request.getParameter("themeid"));
		boolean hot  = Boolean.getBoolean(request.getParameter("hot"));
		String intro =request.getParameter("intro");
		int  hots= Integer.parseInt(request.getParameter("fenshu"));
		SceneDao scene = SceneDao.getscened();
		scene.newscene(cityid, scenename, themeid, hot, intro,hots);
		PrintWriter out = response.getWriter();
		out.print("添加完成！！！");
	}
	

	protected void allscene(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//全部景点
		  Pager pager = Pager.init(request);
		  SceneDao scene = SceneDao.getscened();
		  ArrayList<Scene> allscene = scene.allscene(pager);
		  request.setAttribute("allscene", allscene);

	}
	protected void newphoto(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//添加照片
		ScenePhotoDao  newscene = ScenePhotoDao.getspd();
		//int sceneid = Integer.parseInt(request.getParameter("sceneid"));
		 String  filepath = request.getServletContext().getRealPath("/");
	     MultipartRequest re =new MultipartRequest(request, filepath+"/images/", 2*1024*1024, "utf-8",new FileRename());
	     String scenepic = re.getFilesystemName("scenepic");
	     int sceneid = Integer.parseInt(re.getParameter("sceneid"));
		newscene.newphoto(sceneid, scenepic);
		PrintWriter out = response.getWriter();
		out.print("添加完成！！！");
		
	}  
	protected void deletescene(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//删除选中
	    SceneDao   scene = SceneDao.getscened();
	    String cid[] = request.getParameterValues("cid");
	    scene.deleteall(cid);
	    int a = scene.scenednum();
	    PrintWriter  out = response.getWriter();
	    out.print(a);
    }
	protected void checkall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub\
		String check = request.getParameter("key");
		SceneDao scene = SceneDao.getscened();
		ArrayList<Scene> allcheck = scene.checkscene(check);
		request.setAttribute("allcheck", allcheck);
		
	}
	
	//通过搜索显示景点
	protected void checkallbysearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String check = request.getParameter("key");
		ArrayList<Scene> sc = SceneDao.getscened().checkscene(check);//通过模糊查询返回所有景点，sc
		request.setAttribute("allcheck", sc);
		ArrayList<String> pics = new ArrayList<String>();
		
		for(int i=0;i<sc.size();i++)
		{
			int id = sc.get(i).getId();//获得景点id
			String pic = ScenePhotoDao.getspd().getfirstphotobyscenesid(id);
			pics.add(pic);
		}
		request.setAttribute("pics", pics);
	}
	//
	protected void checkid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		SceneDao scene = SceneDao.getscened();
		int id =Integer.parseInt(req.getParameter("cid"));
		Scene sceneone = scene.getSceneById(id);
		req.setAttribute("sceneone", sceneone);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void modify(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		int cityid = Integer.parseInt(request.getParameter("cityid"));
		String scenename = request.getParameter("scenename");
		int themeid = Integer.parseInt(request.getParameter("themeid"));
		boolean hot  = Boolean.getBoolean(request.getParameter("hot"));
		String intro =request.getParameter("intro");
		int id = Integer.parseInt(request.getParameter("id"));
		int hotscore = Integer.parseInt(request.getParameter("hotscore"));
		SceneDao scenedao = SceneDao.getscened();
		
		scenedao.modify(cityid, scenename, themeid, intro, hot, id,hotscore);
		 PrintWriter  out = resp.getWriter();
		    out.print("修改完成！！！");
	}
	protected void deleteone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		SceneDao scenedao = SceneDao.getscened();
		int id = Integer.parseInt(req.getParameter("id"));
		scenedao.deleteone(id);
		 int a = scenedao.scenednum();
		    PrintWriter  out = resp.getWriter();
		    out.print(a);
	}
	@Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		
	}
	protected void allscenes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		ArrayList<Scene> scene = SceneDao.getscened().allscenes();
		req.setAttribute("scenes", scene);
	}
	

}
